package controllers.listeners.semrep;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import controllers.MainModuleController;

import objects.Constituent;
import views.MainPanel;

/* Deletes the selected constituent */
/* actionPerformed when you click the "Delete Constituent" button */
public class DeleteConstituentActionListener implements ActionListener {
	
	MainPanel view;
	
	public DeleteConstituentActionListener() {
		view = null;
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (view.getCurrCon() != null) {
			if (JOptionPane.showConfirmDialog(null, "Are you sure you want to delete the phrase " + view.getCurrCon().getLabel() + "?",
					"WARNING", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
				Constituent parent = view.getCurrCon().getParent();
				
				for (int i = 0; i < parent.getConstituentList().size(); i++) {
					if (parent.getConstituentList().get(i).equals(view.getCurrCon())) {
						parent.getConstituentList().remove(i);
					}
				}
				view.clearPanels();
				view.loadSemantic(MainModuleController.setTextAreaSemantic(view.getConstituent(), view));
			}
		}
	}
	
	public void setView(MainPanel view) {
		this.view = view;
	}
}
